package com.fjl.gson.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonUtil
{
	// 将Json数据解析成相应的映射对象
	public static <T> T parseJsonWithGson(String jsonData, Class<T> type)
	{
		Gson gson = new Gson();
		T result = gson.fromJson(jsonData, type);
		return result;
	}

	// 将Json数组解析成相应的映射对象列表
	public static <T> List<T> parseJsonArrayWithGson(String jsonData, Class<T> type)
	{
		Gson gson = new Gson();
		List<T> result = gson.fromJson(jsonData, new TypeToken<List<T>>()
		{
		}.getType());
		return result;
	}

	public static <T> List<T> getPersons(String jsonString, Class<T> cls)
	{
		List<T> list = new ArrayList<T>();
			Gson gson = new Gson();
			list = gson.fromJson(jsonString, new TypeToken<List<T>>()
			{
			}.getType());
		return list;
	}

	public static List<Map<String, Object>> listKeyMaps(String jsonString)
	{
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try
		{
			Gson gson = new Gson();
			list = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>()
			{
			}.getType());
		}
		catch (Exception e)
		{
			// TODO: handle exception
		}
		return list;
	}
}
